Introduction to Embedded Systems

Download Report

Transcript Introduction to Embedded Systems

Resource Management in (Embedded)
Real-Time Systems - II
Lecture 18
Introduction to Embedded Systems
Summary of Previous Lecture
• Introduction to Real-Time Systems
–
–
–
–
Terminology
Example Categories
Metrics
Scheduling Policies
Introduction to Embedded Systems
Thought For the Day
You will become as small as your controlling desire,
as great as your dominant aspiration.
– James Allen
Introduction to Embedded Systems
Rate Monotonic Scheduling (RMS)
• Priorities of periodic tasks are based on their rates: highest rate gets
highest priority.
• Theoretical basis
– optimal fixed scheduling policy (when deadlines are at end of period)
– analytic formulas to check schedulability
• Must distinguish between scheduling and analysis
– rate monotonic scheduling forms the basis for rate monotonic analysis
– however, we consider later how to analyze systems in which rate monotonic
scheduling is not used
– any scheduling approach may be used, but all real-time systems should be
analyzed for timing
Introduction to Embedded Systems
Rate Monotonic Analysis (RMA)
• Rate-monotonic analysis is a set of mathematical techniques for
analyzing sets of real-time tasks.
• Basic theory applies only to independent, periodic tasks, but has been
extended to address
– priority inversion
– task interactions
– aperiodic tasks
• Focus is on RMA, not RMS
Introduction to Embedded Systems
Why Are Deadlines Missed?
• For a given task, consider
– preemption: time waiting for higher priority tasks
– execution: time to do its own work
– blocking: time delayed by lower priority tasks
• The task is schedulable if the sum of its preemption, execution, and
blocking is less than its deadline.
• Focus: identify the biggest hits among the three and reduce, as needed, to
achieve schedulability
Introduction to Embedded Systems
Example of Priority Inversion
Collision check: {... P ( ) ... V ( ) ...}
Update location: {... P ( ) ... V ( ) ...}
Attempts to lock data
resource (blocked)
Collision
check
B
Refresh
screen
Update
location
Introduction to Embedded Systems
Rate Monotonic Theory - Experience
• Supported by several standards
– POSIX Real-time Extensions
• Various real-time versions of Linux
– Java (Real-Time Specification for Java and Distributed Real-Time
Specification for Java)
– Real-Time CORBA
– Real-Time UML
– Ada 83 and Ada 95
– Windows 95/98
– …
Introduction to Embedded Systems
Summary
• Real-time goals are:
– fast response,
– guaranteed deadlines, and
– stability in overload.
• Any scheduling approach may be used, but all real-time systems should
be analyzed for timing.
• Rate monotonic analysis
–
–
–
–
based on rate monotonic scheduling theory
analytic formulas to determine schedulability
framework for reasoning about system timing behavior
separation of timing and functional concerns
• Provides an engineering basis for designing real-time systems
Introduction to Embedded Systems
Plan for Lectures
• Present basic theory for periodic task sets
• Extend basic theory to include
– context switch overhead
– preperiod deadlines
– interrupts
• Consider task interactions:
– priority inversion
– synchronization protocols (time allowing)
• Extend theory to aperiodic tasks:
– sporadic servers (time allowing)
Introduction to Embedded Systems
A Sample Problem
Periodics
Servers
Emergency
100 msec
t1
50 msec
20 msec
Data Server
2 msec
150 msec
t2
5 msec
Deadline 6 msec
after arrival
20 msec
40 msec
Comm Server
350 msec
t3
Aperiodics
Routine
40 msec
10 msec
10 msec
2 msec
100 msec
Desired response
20 msec average
t2’s deadline is 20 msec before the end of each period
Introduction to Embedded Systems
Rate Monotonic Analysis
•
•
•
•
•
Introduction
Periodic tasks
Extending basic theory
Synchronization and priority inversion
Aperiodic servers
Introduction to Embedded Systems
A Sample Problem - Periodics
Periodics
Servers
Aperiodics
Emergency
100 msec
t1
50 msec
20 msec
Data Server
2 msec
150 msec
t2
Deadline 6 msec
after arrival
20 msec
40 msec
Comm Server
350 msec
t3
5 msec
Routine
40 msec
10 msec
10 msec
2 msec
100 msec
Desired response
20 msec average
t2’s deadline is 20 msec before the end of each period
Introduction to Embedded Systems
Concepts and Definitions - Periodics
• Periodic task
– initiated at fixed intervals
– must finish before start of next cycle
• Task’s CPU utilization:
Ci
Ui =
Ti
– Ci = worst-case compute time (execution time) for task ti
– Ti = period of task ti
• CPU utilization for a set of tasks
U = U1 + U2 +...+ Un
Introduction to Embedded Systems
Example of Priority Assignment
1
UIP = 10 = 0.10
11
VIP: UVIP = 25 = 0.44
IP:
Semantics-Based Priority Assignment
VIP:
0
25
misses deadline
IP:
0
10
20
30
Policy-Based Priority Assignment
IP:
0
10
20
30
VIP:
0
25
Introduction to Embedded Systems
Schedulability: UB Test
• Utilization bound (UB) test: a set of n independent periodic tasks
scheduled by the rate monotonic algorithm will always meet its
deadlines, for all task phasings, if
U(1) = 1.0
U(2) = 0.828
U(3) = 0.779
U(4) = 0.756
U(5) = 0.743
U(6) = 0.734
U(7) = 0.728
U(8) = 0.724
U(9) = 0.720
• For harmonic task sets, the utilization bound is U(n)=1.00 for all n.
C1
Cn
1/ n
--- + .... + --< U(n) = n(2 - 1)
T1
Tn
Introduction to Embedded Systems
Sample Problem: Applying UB Test
C
T
U
Task t1
20
100
0.200
Task t2
40
150
0.267
Task t3
100
350
0.286
• Total utilization is .200 + .267 + .286 = .753 < U(3) = .779
• The periodic tasks in the sample problem are schedulable according to
the UB test
Introduction to Embedded Systems
Timeline for Sample Problem
0
100
200
300
400
t1
t2
t3
Scheduling Points
Introduction to Embedded Systems
Exercise: Applying the UB Test
Given:
Task
t
t2
t
C
1
2
1
T
4
6
10
U
a. What is the total utilization?
b. Is the task set schedulable?
c. Draw the timeline.
d. What is the total utilization if C3 = 2 ?
Introduction to Embedded Systems
Solution: Applying the UB Test
a. What is the total utilization? .25 + .34 + .10 = .69
b. Is the task set schedulable? Yes: .69 < U(3) = .779
c. Draw the timeline.
0
5
10
15
20
Task t
Task t2
Task t
d. What is the total utilization if C3 = 2 ?
.25 + .34 + .20 = .79 > U(3) = .779
Introduction to Embedded Systems
Toward a More Precise Test
• UB test has three possible outcomes:
0 < U < U(n)
 Success
U(n) < U < 1.00
 Inconclusive
1.00 < U
 Overload
• UB test is conservative.
• A more precise test can be applied.
Introduction to Embedded Systems
Schedulability: RT Test
• Theorem: The worst-case phasing of a task occurs when it arrives
simultaneously with all its higher priority tasks.
• Theorem: for a set of independent, periodic tasks, if each task meets its first
deadline, with worst-case task phasing, the deadline will always be met.
• Response time (RT) or Completion Time test: let an = response time of
task i. an of task I may be computed by the following iterative formula:
i- 1
a n+1 = C i +

j = 1
an
Tj
i
Cj
where a 0 =

j = 1
Cj
• Test terminates when an+1 = an.
• Task i is schedulable if its response time is before its deadline: an < Ti
• The above must be repeated for every task i from scratch
• This test must be repeated for every task ti if required
• i.e. the value of i will change depending upon the task you are looking at
• Stop test once current iteration yields a value of an+1 beyond the deadline (else,
you may never terminate).
• The ‘square bracketish’ thingies represent the ‘ceiling’ function, NOT brackets
Introduction to Embedded Systems
Example: Applying RT Test -1
• Taking the sample problem, we increase the compute time of t1 from 20 to
40; is the task set still schedulable?
Task t:
Task t2:
Task t:
C
20 40
40
100
T
100
150
350
U
0.200 0.4
0.267
0.286
• Utilization of first two tasks: 0.667 < U(2) = 0.828
– first two tasks are schedulable by UB test
• Utilization of all three tasks: 0.953 > U(3) = 0.779
– UB test is inconclusive
– need to apply RT test
Introduction to Embedded Systems
Example: Applying RT Test -2
•Use RT test to determine if t3 meets its first deadline: i = 3
3
a
0
=
 Cj =
C +C +C
1
2
3
= 40 + 40 + 100 = 180
j=1
i- 1
a1 = C i +

j = 1
a0
Tj
2
Cj = C3 +

j = 1
a0
Tj
Cj
= 100 + 180 ( 40 ) + 180 ( 40 ) = 100 + 80 + 80 = 260
100
150
Introduction to Embedded Systems
Example: Applying the RT Test -3
2 a1
C = 100 + 260 (40) + 260 (40) = 00
a =C + 
j
2
3
100
150
j = 1 Tj
2 a2
a =C + 
C = 100 + 300 (40) + 300 (40) = 00
3
3
j
T
100
150
j =1 j
a3 = a2 = 300 Done!
•Task t3 is schedulable using RT test
a 3 = 300 < T = 350
Introduction to Embedded Systems
Timeline for Example
0
100
200
300
t1
t2
t3
t 3 completes its work at t = 300
Introduction to Embedded Systems
Exercise: Applying RT Test
Task t1: C1 = 1 T1 = 4
Task t2: C2 = 2 T2 = 6
Task t3: C3 = 2 T3 = 10
a) Apply the UB test
b) Draw timeline
c) Apply RT test
Introduction to Embedded Systems
Solution: Applying RT Test
a) UB test
t and t2 OK -- no change from previous exercise
.25 + .34 + .20 = .79 > .779 ==> Test inconclusive for t
b) RT test and timeline
0
5
10
15
20
Task t
Task t2
Task t
All work completed at t = 6
Introduction to Embedded Systems
Solution: Applying RT Test
(cont.)
c) RT test
3
a
0
=
 Cj =
C +C +C
1
2
3
= 1 +2 +2
= 5
j=1
2
a 1 = C3 +

j = 1
2
a 2 = C3 +

j = 1
a0
Tj
a1
Tj
Cj = 2 +
5
Cj = 2 +
6
1 +
2 = 2+2+2 = 6
6
4
4
5
1 +
6
2 = 2+2+2 = 6
6
Done
Introduction to Embedded Systems
Summary
• UB test is simple but conservative.
• RT test is more exact but also more complicated.
• To this point, UB and RT tests share the same limitations:
–
–
–
–
–
–
–
all tasks run on a single processor
all tasks are periodic and noninteracting
deadlines are always at the end of the period
there are no interrupts
Rate-monotonic priorities are assigned
there is zero context switch overhead
tasks do not suspend themselves
Introduction to Embedded Systems
Sample Problem: Applying UB Test
C
T
U
Task t:
20
100
0.200
Task t2:
40
150
0.267
Task t:
100
350
0.286
• Total utilization is .200 + .267 + .286 = .753 < U(3) = .779
• The periodic tasks in the sample problem are schedulable according to
the UB test
Introduction to Embedded Systems
Timeline for Sample Problem
0
100
200
300
400
t1
t2
t3
Scheduling Points
Introduction to Embedded Systems
Exercise: Applying the UB Test
Given:
Task
t
t2
t
C
1
2
1
T
4
6
10
U
a. What is the total utilization?
b. Is the task set schedulable?
c. Draw the timeline.
d. What is the total utilization if C3 = 2 ?
Introduction to Embedded Systems
Solution: Applying the UB Test
a. What is the total utilization? .25 + .34 + .10 = .69
b. Is the task set schedulable? Yes: .69 < U(3) = .779
c. Draw the timeline.
0
5
10
15
20
Task t
Task t2
Task t
d. What is the total utilization if C3 = 2 ?
.25 + .34 + .20 = .79 > U(3) = .779
Introduction to Embedded Systems
Towards a More Precise Test
• UB test has three possible outcomes:
0 < U < U(n)
 Success
U(n) < U < 1.00
 Inconclusive
1.00 < U
 Overload
• UB test is conservative.
• A more precise test can be applied.
Introduction to Embedded Systems
Schedulability: RT Test
• Theorem: The worst-case phasing of a task occurs when it arrives
simultaneously with all its higher priority tasks.
• Theorem: for a set of independent, periodic tasks, if each task meets its first
deadline, with worst-case task phasing, the deadline will always be met.
• Response time (RT) or Completion Time test: let an = response time of
task i. an may be computed by the following iterative formula:
i- 1
a n+1 = C i +

j = 1
an
Tj
i
Cj
where a 0 =

j = 1
Cj
• Test terminates when an+1 = an.
• Task i is schedulable if its response time is before its deadline: an < Ti
• This test must be repeated for every task ti (if required)
• i.e. the value of i will change depending upon the task you are looking at
• Stop test once current iteration yields a value of an+1 beyond the deadline (else,
you may never terminate).
• The ‘square bracketish’ thingies constitute the ‘ceiling’ function:
• ceiling (x) = smallest integer greater than or equal to x
Introduction to Embedded Systems
Example: Applying RT Test -1
• Taking the sample problem, we increase the compute time of t1 from 20 to
40; is the task set still schedulable?
Task t:
Task t2:
Task t:
C
20 40
40
100
T
100
150
350
U
0.200 0.4
0.267
0.286
• Utilization of first two tasks: 0.667 < U(2) = 0.828
– first two tasks are schedulable by UB test
• Utilization of all three tasks: 0.953 > U(3) = 0.779
– UB test is inconclusive
– need to apply RT test
Introduction to Embedded Systems
Example: Applying RT Test -2
•Use RT test to determine if t3 meets its first deadline: i = 3
3
a
0
=
 Cj =
C +C +C
1
2
3
= 40 + 40 + 100 = 180
j=1
i- 1
a1 = C i +

j = 1
a0
Tj
2
Cj = C3 +

j = 1
a0
Tj
Cj
= 100 + 180 ( 40 ) + 180 ( 40 ) = 100 + 80 + 80 = 260
100
150
Introduction to Embedded Systems
Example: Applying the RT Test -3
2 a1
C = 100 + 260 (40) + 260 (40) = 00
a =C + 
j
2
3
100
150
j = 1 Tj
2 a2
a =C + 
C = 100 + 300 (40) + 300 (40) = 00
3
3
j
T
100
150
j =1 j
a3 = a2 = 300 Done!
•Task t3 is schedulable using RT test
a 3 = 300 < T = 350
Introduction to Embedded Systems
Timeline for Example
0
100
200
300
t1
t2
t3
t 3 completes its work at t = 300
Introduction to Embedded Systems
Exercise: Applying RT Test
Task t1: C1 = 1 T1 = 4
Task t2: C2 = 2 T2 = 6
Task t3: C3 = 2 T3 = 10
a) Apply the UB test
b) Draw timeline
c) Apply RT test
Introduction to Embedded Systems
Solution: Applying RT Test
a) UB test
t and t2 OK -- no change from previous exercise
.25 + .34 + .20 = .79 > .779 ==> Test inconclusive for t
b) RT test and timeline
0
5
10
15
20
Task t
Task t2
Task t
All work completed at t = 6
Introduction to Embedded Systems
Solution: Applying RT Test
(cont.)
c) RT test
3
a
0
=
 Cj =
C +C +C
1
2
3
= 1 +2 +2
= 5
j=1
2
a 1 = C3 +

j = 1
2
a 2 = C3 +

j = 1
a0
Tj
a1
Tj
Cj = 2 +
5
Cj = 2 +
6
1 +
2 = 2+2+2 = 6
6
4
4
5
1 +
6
2 = 2+2+2 = 6
6
Done
Introduction to Embedded Systems
Summary
• UB test is simple but conservative.
• RT test is more exact but also more complicated.
• To this point, UB and RT tests share the same limitations:
–
–
–
–
–
–
–
all tasks run on a single processor
all tasks are periodic and noninteracting
deadlines are always at the end of the period
there are no interrupts
Rate-monotonic priorities are assigned
there is zero context switch overhead
tasks do not suspend themselves
Introduction to Embedded Systems
Rate Monotonic Analysis
•
•
•
•
•
Introduction
Periodic tasks
Extending the basic theory
Synchronization and Priority Inversion
Aperiodic servers
Introduction to Embedded Systems
A Sample Problem
Periodics
Servers
Aperiodics
Emergency
100 msec
t1
50 msec
20 msec
Data Server
2 msec
150 msec
t2
Deadline 6 msec
after arrival
20 msec
40 msec
Comm Server
350 msec
t3
5 msec
10 msec
10 msec
Routine
40 msec
2 msec
100 msec
Desired response
20 msec average
t2’s deadline is 20 msec before the end of each period
Introduction to Embedded Systems
Extensions to Basic Theory
• This section extends the schedulability tests to address
– nonzero task switching times
– preperiod deadlines
– interrupts and non-rate-monotonic priorities
Introduction to Embedded Systems
Modeling Task Switching as Execution Time
Ci + 2S
Ui =
Ti
0
t
C1
t2
100
200
C1
C1
C2
C1
C2
C2
t
S
40
C1
C1 S
S
C2 S
S
time
40
T1
T2
2T1
Two scheduling actions per task
(start of period and end of period)
Introduction to Embedded Systems
Modeling Preperiod Deadlines
•
•
Suppose task t, with compute time C and period T, has a preperiod
deadline D (i.e. D < T ).
Compare total utilization to modified bound:
Utotal =
C1
T1
+ ... +
Cn
Tn
 U(n, Di)
where Di is the ratio (Di / Ti).
 n( (2D ) 1 n - 1) + 1 - D , 1 < D  1.0 
i
i 2 i

÷
U(n, Di) = 
÷
1
Di , Di  2

Introduction to Embedded Systems
Schedulability with Interrupts
• Interrupt processing can be inconsistent with rate-monotonic priority
assignment.
– interrupt handler executes with high priority despite its period
– interrupt processing may delay execution of tasks with shorter periods
• Effects of interrupt processing must be taken into account in
schedulability model.
• Question is: how to do that?
Introduction to Embedded Systems
Example: Determining Schedulability with Interrupts
Task t1:
Task t2:
Task t3:
Task t4:
C
20
40
60
40
T
100
150
200
350
U
0.200
0.267
0.300
0.115
t3 is an interrupt handler
Introduction to Embedded Systems
Example: Execution with Rate-Monotonic Priorities
0
00
200
00
400
t
t2
t
t4
Introduction to Embedded Systems
Example: Execution with an Interrupt Priority
0
00
200
00
400
t
t2
Interrupt
t4
Introduction to Embedded Systems
Resulting Table for Example
Task
(i)
Period
(T)
Execution
Time (C)
Priority
(P)
Deadline
(D)
t3
200
60
Hardware
(highest)
200
t1
100
20
High
100
t2
150
40
Medium
150
t4
350
40
Low
350
Introduction to Embedded Systems
UB Test with Interrupt Priority
• Test is applied to each task.
• Determine effective utilization (fi) of each task ti using
i

Tj

j Hn
Preemption
from tasks that
can “hit” more than once
(with period less than Di)
+
Ci
Ti
+
C

Ti k H k
1

f =
Cj
Execution of
task under test
1
Preemption
from tasks that
can hit only once
(with period greater
than Di)
Compare effective utilization against bound U(n).
• n = num(Hn) + 1
• num(Hn) = the number of tasks in the set Hn
Introduction to Embedded Systems
UB Test with Interrupt Priority: t3
• For t3, no tasks have a higher priority:
– H = Hn = H1 = { }
f
3
=
C3
 0 + T 3+  0
Note:
num(Hn) = 0; therefore, utilization bound is U(1).
Plugging in the numbers:
f3 =
C3
T3
60
=
= 0.3 < 1.0
200
Introduction to Embedded Systems
UB Test with Interrupt Priority: t1
To t1, t3 has higher priority: H = {t3 }; Hn = { }; H1 = {t3 }
f =
1

C1
1
0+
+
T1 T1
Ck

k=3
Note:
num(Hn) = 0; therefore, utilization bound is U(1).
Plugging in the numbers:
f1 =
C1
T1
+
C3
T1
=
20
60
+
= 0.800 < 1.0
100 100
Introduction to Embedded Systems
UB Test with Interrupt Priority: t2
To t2: H = {t,t}; Hn = {t}; H1 = {t}.

Cj
C2
1
f =
+
+
2
T2
j = 1T j T2
Ck

k=3
Note:
num(Hn) = 1; therefore, utilization bound is U(2).
Plugging in the numbers:
C1 C2 C3 20 40
f2 = + + = + + 60 = 0.867 > 0.828
T1 T2 T2 100 150 150
Introduction to Embedded Systems
UB Test with Interrupt Priority: t4
To t4: H = {t1, t2, t3}; Hn = {t1, t2, t3}; H1 = { }.
f =
4
 T j + T4 +  0
Cj
C4
j = 1,2,3
Note:
num(Hn) = 3; therefore, utilization bound is U(4).
Plugging in the numbers:
C1 C2 C3 C4
f4 = + + +
T1 T2 T3 T4
20 + 40 + 60 40
=
+
= 0.882 > 0.756
100 150 200 350
Introduction to Embedded Systems
Exercise: Schedulability with Interrupts
• Use the UB test to determine which tasks are schedulable
• Given the following tasks:
Task Period Execution Deadline Priority
(i)
(T)
Time
(D)
(P)
(C)
6
2
HW
6
tint
4
1
High
3
t2
10
1
Low
10
t
Introduction to Embedded Systems
Solution: Schedulability with Interrupts
Cint
 U (1)
0.334 < 1.0
Tint
{H1}
C1
+
T1
Cint
 U (1, .75)
0.250 + 0.500 = 0.750 = U(1, .75)
T1
{Hn}
Cint
Tint
+
C1
T1
+
C2
T2
 U (3)
0. 334 + 0. 250 + 0.100 = 0.684 < 0.779
Introduction to Embedded Systems
Basic Theory: Where Are We?
• We have shown how to handle
– task context switching time: include 2S in C
– Pre-period deadlines: change bound to U(n, Di)
– non-rate-monotonic priority assignments
• We still must address
– task interactions
– aperiodic tasks
• We still assume
– single processor
– priority-based scheduling
– a task does not suspend itself voluntarily
Introduction to Embedded Systems